﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Client
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void btnLink_Click(object sender, EventArgs e)
		{

		}

		private void btnSendMsg_Click(object sender, EventArgs e)
		{
			string message = txtMsg.Text;
			txtInfo.AppendText(message + Environment.NewLine);
			try
			{
				TcpClient tcpClient = new TcpClient();
				tcpClient.Connect(IPAddress.Parse(txtHostName.Text), Int32.Parse(txtPort.Text));
				NetworkStream ns = tcpClient.GetStream();
				if (ns.CanWrite)
				{
					Byte[] sendBytes = Encoding.UTF8.GetBytes(message);
					ns.Write(sendBytes, 0, sendBytes.Length);
				}
				else
				{
					txtInfo.AppendText("不能写入数据流\r\n");
					tcpClient.Close();
					ns.Close();
					return;
				}
				ns.Close();
				tcpClient.Close();
			}
			catch (Exception ex)
			{
				txtInfo.AppendText("异常: " + ex.Message + Environment.NewLine);
			}

		}

		private void btnSenFile_Click(object sender, EventArgs e)
		{
			TcpClient tcpClient = new TcpClient();
			tcpClient.Connect(IPAddress.Parse(txtHostName.Text), Int32.Parse(txtPort.Text));
			NetworkStream ns = tcpClient.GetStream();
			FileStream fs = File.Open("..\\..\\hhh.txt", FileMode.Open);
			int data = fs.ReadByte();
			while (data != -1)
			{
				ns.WriteByte((byte)data);
				data = fs.ReadByte();
			}
			fs.Close();
			ns.Close();
			tcpClient.Close();
		}

		
	}
}
